package com.example.mywiseelves.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.mywiseelves.bean.ElvesBean;
import com.example.mywiseelves.bean.ResourcesBean;
import com.example.mywiseelves.utils.DBHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ZJK
 * @data 2021 20
 * @description
 */
public class ResourcesDao {
    private final DBHelper dbHelper;

    public ResourcesDao(Context context) {
        //创建数据库
        dbHelper = new DBHelper(context);
    }

    /**
     * 添加资源数据，添加初始图片资源
     */
    public void insertData(int resources_id, int splash_image, int splash_states) {
        //db对象
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        try {
            //开启事务
            db.beginTransaction();
            //存储体
            ContentValues values = new ContentValues();
            //添加数据
            //图片id
            values.put("splash_id", resources_id);
            //图片资源
            values.put("splash_image", splash_image);
            //图片状态
            values.put("splash_states", splash_states);
            //插入数据
            db.insert("resources", null, values);
            //事务成功
            db.setTransactionSuccessful();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //结束事务
            db.endTransaction();
            //关闭数据库
            db.close();
        }
    }

    /**
     * 单条查询,应用在初始选择上
     */
    public int splashQuery() {
        //db对象
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int splash_image = 0;
        try {
            //开启事务
            db.beginTransaction();
            String sql = "select splash_image from resources where splash_states = 1";
            Cursor cursor = db.rawQuery(sql, null);
            // 看看查询的结果是否为空
            if (cursor == null) {
                return 0;
            }
            while (cursor.moveToNext()) {
                splash_image =cursor.getInt(cursor.getColumnIndex("splash_image"));
            }
            //事务成功
            db.setTransactionSuccessful();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //结束事务
            db.endTransaction();
            //关闭数据库
            db.close();
        }
        return splash_image;
    }
    /**
     * 所有查询,应用在设置上，用以显示
     */
    public List<ResourcesBean> allSplashQuery() {
        //db对象
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        List<ResourcesBean> resourcesBeanList = new ArrayList<>();
        ResourcesBean resourcesBean ;
        try {
            //开启事务
            db.beginTransaction();
            String sql = "select * from resources";
            Cursor cursor = db.rawQuery(sql, null);
            while (cursor.moveToNext()) {
                resourcesBean = new ResourcesBean();
                resourcesBean.setSplash_id(cursor.getInt(cursor.getColumnIndex("splash_id")));
                resourcesBean.setSplash_image(cursor.getInt(cursor.getColumnIndex("splash_image")));
                resourcesBean.setSplash_states(cursor.getInt(cursor.getColumnIndex("splash_states")));
                resourcesBeanList.add(resourcesBean);
            }
            cursor.close();
            db.setTransactionSuccessful();
            Log.v("myapp", "查询成功,一共：" + resourcesBeanList.size() + "条数据");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //结束事务
            db.endTransaction();
            //关闭数据库
            db.close();
        }
        return resourcesBeanList;
    }

    /**
     * 资源信息更新，更新状态,根据id,用于在适配器更改启动动画
     */
    public void splashStatesUpdate(ResourcesBean resourcesBean ) {
        //db对象
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        try {
            //开启事务
            db.beginTransaction();
            String[] args = {String.valueOf(resourcesBean.getSplash_id())};
            //存储体
            ContentValues values = new ContentValues();
            //图片状态状态
            values.put("splash_states", 1);
            //更新数据
            db.update("resources", values, "splash_id = ?", args);
            //事务成功
            db.setTransactionSuccessful();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //结束事务
            db.endTransaction();
            //关闭数据库
            db.close();
        }
    }

    /**
     * 资源信息更新所有，更新状态,用于在适配器更改启动动画
     */
    public void allSplashStatesUpdate() {
        //db对象
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        try {
            //开启事务
            db.beginTransaction();
            //存储体
            ContentValues values = new ContentValues();
            //图片状态
            values.put("splash_states", 0);
            //更新数据
            db.update("resources", values, null, null);
            //事务成功
            db.setTransactionSuccessful();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //结束事务
            db.endTransaction();
            //关闭数据库
            db.close();
        }
    }

}
